package com.data.center.service.impl;

import com.data.center.dictionary.AdminDivisionProvince;
import com.data.center.dictionary.AdminDivisionRegion;
import com.data.center.dto.RegionDivisionRelationDto;
import com.data.center.mapper.RegionDivisionRelationMapper;
import com.data.center.service.RegionDivisionRelationService;
import com.data.center.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionDivisionRelationServiceImpl implements RegionDivisionRelationService {

    @Autowired
    private RegionDivisionRelationMapper regionDivisionRelationMapper;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    public void insert(RegionDivisionRelationDto relation) {
        relation.setId(String.valueOf(snowflakeIdWorker.nextId()));
        regionDivisionRelationMapper.insert(relation);
    }

    public void update(RegionDivisionRelationDto relation) {
        regionDivisionRelationMapper.update(relation);
    }

    public void delete(String id) {
        regionDivisionRelationMapper.delete(id);
    }

    public RegionDivisionRelationDto findById(String id) {
        return regionDivisionRelationMapper.findById(id);
    }

    /*
    * 根据输入的行政区域编码，查询其所在的大区编码
    *
    * */
    public RegionDivisionRelationDto findByDivisionCode(String divisionCode) {
        return regionDivisionRelationMapper.findByDivisionCode(divisionCode);
    }

    @Override
    public void updateFromExcel(List<String> inputDatas) {
        for (String inputData : inputDatas) {
            String[] split = inputData.split(",");
            //获取regionCode
            AdminDivisionRegion adminDivisionRegion = AdminDivisionRegion.getObjectByValue(split[0]);
            String regionCode = adminDivisionRegion.getKey() + "000000";
            //遍历所有的省/直辖市名
            for(int i=1;i < split.length; i++){
                AdminDivisionProvince adminDivisionProvince = AdminDivisionProvince.getObjectByValue(split[i]);
                //如果插入值已存在，则跳过该项,如果存在但不相等，就更新regionCode
                if (findByDivisionCode(adminDivisionProvince.getKey()) != null){
                    if (!regionCode.equals(findByDivisionCode(adminDivisionProvince.getKey()).getRegionCode())){
                        RegionDivisionRelationDto regionDivisionRelationDto = new RegionDivisionRelationDto();
                        regionDivisionRelationDto.setId(findByDivisionCode(adminDivisionProvince.getKey()).getId());
                        regionDivisionRelationDto.setRegionCode(regionCode);
                        regionDivisionRelationDto.setDivisionCode(adminDivisionProvince.getKey());
                        update(regionDivisionRelationDto);
                    }
                    continue;
                }
                //插入数据
                RegionDivisionRelationDto regionDivisionRelationDto = new RegionDivisionRelationDto();
                regionDivisionRelationDto.setRegionCode(regionCode);
                regionDivisionRelationDto.setDivisionCode(adminDivisionProvince.getKey());
                insert(regionDivisionRelationDto);
            }


        }

    }

}
